<template>
	<div>
		<el-dialog title="移动稿件" v-model="isShowDialog" >
			<el-form ref="selectChannelFormRef" :model="formData" :rules="formRules" label-width="80px">
				<el-row :gutter="35">
					<el-col class="mb20">
						<el-form-item label="目标栏目" prop="channelId">
							<el-cascader
								placeholder="请选择目标栏目"
								v-model="formData.channelId"
								:options="channels"
								:props="{expandTrigger: 'hover', emitPath: false, checkStrictly: true, value: 'id', label: 'name', children: 'children' }"
								class="w100"
								clearable>
							</el-cascader>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel">取 消</el-button>
					<el-button type="primary" @click="onSubmit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import { ElMessage } from 'element-plus';
import { lineToTreeData } from '/@/utils/commonFunction';
import { getChannelList } from '/@/api/cms/channel/index';
import { moveDocument, mirrorDocument, copyDocument } from '/@/api/cms/document/index';
export default {
	name: 'cms/channel/select',
	setup(props, context) {
		const selectChannelFormRef = ref();
		const state = reactive({
			isShowDialog: false,
			action : '',
			channels: [],
			formData: {
				ids: [],
				siteId: '',
				channelId: '',
			},
			formRules : {
				channelId: { required: true, message: '目标栏目不能为空', trigger: 'change' },
			}
		});

		// 打开弹窗
		const openDialog = (row: any, action: string) => {
			state.isShowDialog = true;
			state.action = action;
			state.formData.ids = row.ids;
			getChannelList({siteId: row.siteId}).then((res) => {
				if(res.code == 0){
					if(state.action == 'mirror'){
						res.data.forEach(item => {
							if(item.parentId == '0' || row.channelId == item.id){
								item.disabled = true;
							}
						});
					}else{
						res.data.forEach(item => {
							if(item.parentId == '0'){
								item.disabled = true;
							}
						});
					}
					state.channels = lineToTreeData(res.data);
				}
			});
		};
		// 关闭弹窗
		const closeDialog = () => {
			state.isShowDialog = false;
			state.action = '';
			for(var key in state.formData) {
				state.formData[key] = undefined;
			}
			selectChannelFormRef.value.resetFields();
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		// 新增
		const onSubmit = () => {
			selectChannelFormRef.value.validate(valid => {
				if(valid){
					let params = {
						ids: state.formData.ids,
						channelId: state.formData.channelId,
					}
					switch(state.action){
						case 'move':
							onMoveDocument(params);
							break;
						case 'mirror':
							onMirrorDocument(params);
							break;
						case 'copy':
							onCopyDocument(params);
							break;
						default:
							break;
					}
				}
			});
		};
		
		const onMoveDocument = (params: object) => {
			moveDocument(params).then((res) => {
				if(res.code === 0){
					ElMessage.success('移动成功');
					context.emit('submit');
					closeDialog();
				}
			});
		}
		
		const onMirrorDocument = (params: object) => {
			mirrorDocument(params).then((res) => {
				if(res.code === 0){
					ElMessage.success('引用成功');
					context.emit('submit');
					closeDialog();
				}
			});
		}
		
		const onCopyDocument = (params: object) => {
			copyDocument(params).then((res) => {
				if(res.code === 0){
					ElMessage.success('复制成功');
					context.emit('submit');
					closeDialog();
				}
			});
		}
		return {
			selectChannelFormRef,
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			...toRefs(state),
		};
	},
};
</script>

<style>
</style>
